import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { hashHistory, Route, Router, IndexRoute } from 'react-router';

import { hasToken, login } from './service/Auth';
import { currUser } from './model/CurrUserModel';

import { Login } from './view/Login';
import { Main } from './view/Main';

import { ContentPageNavigation } from './view/content/index';
import { SettingPageNavigation } from './view/setting/index';

import { PageManager } from './view/content/pages/PageManager';
import { PageEditor } from './view/content/pages/PageEditor';
import { ContentEditor } from './view/content/pages/standard/ContentEditor';

import { BannerManager } from './view/content/assets/standard/banner/Manager';
import { BannerEditor } from './view/content/assets/standard/banner/Editor';
import { AdsManager } from './view/content/assets/standard/ads/Manager';
import { AdsEditor } from './view/content/assets/standard/ads/Editor';
import { TopicManager } from './view/content/assets/standard/topic/Manager';
import { TopicEditor } from './view/content/assets/standard/topic/Editor';
import { EventsManager } from './view/content/assets/standard/event/Manager';
import { EventEditor } from './view/content/assets/standard/event/Editor';

import { AccountManager } from './view/setting/account/Manager';
import { CreateAccount } from './view/setting/account/CreateAccount';
import { AccountEditor } from './view/setting/account/Editor';
import { MediaManager } from './view/setting/media/Manager';
import { MediaEditor } from './view/setting/media/Editor';

import '../src/css/style.scss';

const requireAuth = (nextState, replace) => {
    if (!hasToken()) {
        replace({
            pathname: '/login',
            state: {nextPathname: nextState.location.pathname}
        })
    }
};

ReactDOM.render(
    <Router history={hashHistory}>
        <Route path="/" component={Main} onEnter={requireAuth}>
            <Route path="content" component={ContentPageNavigation}>
                <Route path="page" >
                    <Route path="manager" component={PageManager} />
                    <Route path="create" component={PageEditor} />
                    <Route path="edit/:id" component={PageEditor} />
                </Route>
                <Route path="assets">
                    <Route path="banner/manager" component={BannerManager} />
                    <Route path="banner/edit/:id" component={BannerEditor} />
                    <Route path="banner/create" component={BannerEditor} />
                    <Route path="ads/manager" component={AdsManager} />
                    <Route path="ads/create" component={AdsEditor} />
                    <Route path="ads/edit/:id" component={AdsEditor} />
                    <Route path="topic/manager" component={TopicManager} />
                    <Route path="topic/create" component={TopicEditor} />
                    <Route path="topic/edit/:id" component={TopicEditor} />
                    <Route path="event/manager" component={EventsManager} />
                    <Route path="event/create" component={EventEditor} />
                    <Route path="event/edit/:id" component={EventEditor} />
                </Route>
            </Route>
            <Route path="setting" component={SettingPageNavigation}>
                <Route path="account">
                    <Route path="manager" component={AccountManager} />
                    <Route path="create" component={CreateAccount} />
                    <Route path="edit/:id" component={AccountEditor} />
                    <Route path="me" component={AccountEditor} />
                </Route>
                <Route path="media">
                    <Route path="manager" component={MediaManager} />
                    <Route path="create" component={MediaEditor} />
                    <Route path="edit/:id" component={MediaEditor} />
                </Route>
            </Route>
        </Route>

        <Route path="/login" component={Login} onEnter={(s, replace) => hasToken() && replace("/content")} />
    </Router>,
    document.querySelector('application')
);